﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Runtime.Serialization;

namespace JWUX.Supermarket.Model
{
    public enum RequestsChangedType
    {
        Add, Remove
    }

    public class RequestsCountChangeEventArgs : EventArgs
    {
        public RequestsChangedType Type
        {
            get;
            set;
        }

        public RequestInfo Request
        {
            get;
            set;
        }
    }

    public class RequestsCollection : IDictionary<string, RequestInfo>
    {
        #region 成员

        private Dictionary<string, RequestInfo> dic = new Dictionary<string, RequestInfo>();

        #endregion

        #region 事件

        public delegate void CountChangedEventHandler(object sender, RequestsCountChangeEventArgs e);
        public event CountChangedEventHandler CountChanged;

        public void OnChange(RequestsCountChangeEventArgs e)
        {
            if (CountChanged != null)
            {
                this.CountChanged(this, e);
            }
        }

        #endregion

        #region 方法

        public RequestsCollection()
            : base()
        { }

        public void Add(string key, RequestInfo value)
        {
            dic.Add(key, value);
            OnChange(new RequestsCountChangeEventArgs
            {
                Type = RequestsChangedType.Add,
                Request = value
            });
        }

        public bool ContainsKey(string key)
        {
            return dic.ContainsKey(key);
        }

        public ICollection<string> Keys
        {
            get { return dic.Keys; }
        }

        public bool Remove(string key)
        {
            try
            {
                RequestInfo removed = dic[key];
                dic.Remove(key);
                OnChange(new RequestsCountChangeEventArgs
                {
                    Type = RequestsChangedType.Remove,
                    Request = removed
                });
                return true;
            }
            catch { return false; }
        }

        public bool TryGetValue(string key, out RequestInfo value)
        {
            return dic.TryGetValue(key, out value);
        }

        public ICollection<RequestInfo> Values
        {
            get { return dic.Values; }
        }

        public RequestInfo this[string key]
        {
            get { return dic[key]; }
            set { this[key] = value; }
        }

        public void Add(KeyValuePair<string, RequestInfo> item)
        {
            ((ICollection<KeyValuePair<string, RequestInfo>>)dic).Add(item);
            OnChange(new RequestsCountChangeEventArgs
            {
                Type = RequestsChangedType.Add,
                Request = item.Value
            });
        }

        public void Clear()
        {
            dic.Clear();
        }

        public bool Contains(KeyValuePair<string, RequestInfo> item)
        {
            return false;
        }

        public void CopyTo(KeyValuePair<string, RequestInfo>[] array, int arrayIndex)
        {
            ((ICollection<KeyValuePair<string, RequestInfo>>)dic).CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return dic.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<string, RequestInfo> item)
        {
            try
            {
                ((ICollection<KeyValuePair<string, RequestInfo>>)dic).Remove(item);
                OnChange(new RequestsCountChangeEventArgs
                {
                    Type = RequestsChangedType.Remove,
                    Request = item.Value
                });
                return true;
            }
            catch { return false; }
        }

        public IEnumerator<KeyValuePair<string, RequestInfo>> GetEnumerator()
        {
            return ((ICollection<KeyValuePair<string, RequestInfo>>)dic).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return dic.GetEnumerator();
        }

        #endregion
}
}
